package com.kaishengit.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.dao.CompDao;
import com.kaishengit.dao.SaleDao;
import com.kaishengit.dao.TrDao;
import com.kaishengit.entity.All;
import com.kaishengit.entity.Comp;
import com.kaishengit.entity.Page;
import com.kaishengit.entity.Tr;
import com.kaishengit.entity.User;
import com.kaishengit.util.Zhuanma;

public class AddServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	UUID uuid = UUID.randomUUID();

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String task = request.getParameter("task");
		String info=request.getParameter("info");
		String method = request.getMethod();
		if (task == null || "".equals(task)) {

		} else if ("add".equals(task)&&"admin".equals(info)) {
			if ("get".equalsIgnoreCase(method)) {
				toadd(request, response);
			} else if ("post".equalsIgnoreCase(method)) {
				add(request, response);
			}
		}else if("add".equals(task)&&"worker".equals(info)){
			if ("get".equalsIgnoreCase(method)) {
				toadd(request, response);
			} else if ("post".equalsIgnoreCase(method)) {
				workerAdd(request, response);
			}
			
		}
	}

	private void workerAdd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
					String time = request.getParameter("time");
			String danwei = Zhuanma.IsoToUtf(request.getParameter("danwei"));
			String tr = Zhuanma.IsoToUtf(request.getParameter("tr"));
			int num = Integer.parseInt(request.getParameter("num"));
			String from = Zhuanma.IsoToUtf(request.getParameter("from"));
			float total = Float.parseFloat(request.getParameter("total"));
			float unitmoney = total / num;

			SaleDao saledao = new SaleDao();

			CompDao compdao = new CompDao();
			Comp comp = compdao.findByComName(danwei);

			TrDao trdao = new TrDao();
			Tr t = trdao.findByTrName(tr);

			if (comp == null) {
				comp = new Comp();
				comp.setId(UUID.randomUUID().toString());
				comp.setCompname(danwei);
				compdao.save(comp.getId(), comp.getCompname());
			}
			if (t == null) {
				t = new Tr();
				t.setId(UUID.randomUUID().toString());
				t.setTrname(tr);
				trdao.save(t.getId(), tr, null);
			}

			saledao.add(UUID.randomUUID().toString(), time, comp.getId(),
					t.getId(), from, total, num, unitmoney);
			request.getRequestDispatcher("WEB-INF/view/add.jsp").forward(request,
					response);
			
		}
		

	private void toadd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session=request.getSession();
		Object o=session.getAttribute("user");
		User user=(User)o;
		request.setAttribute("juese", user.getInfo());
		request.setAttribute("user", user);
		request.getRequestDispatcher("WEB-INF/view/add.jsp").forward(request,
				response);

	}

	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String time = request.getParameter("time");
		String danwei = Zhuanma.IsoToUtf(request.getParameter("danwei"));
		String tr = Zhuanma.IsoToUtf(request.getParameter("tr"));
		int num = Integer.parseInt(request.getParameter("num"));
		String from = Zhuanma.IsoToUtf(request.getParameter("from"));
		float total = Float.parseFloat(request.getParameter("total"));
		float unitmoney = total / num;

		SaleDao saledao = new SaleDao();

		CompDao compdao = new CompDao();
		Comp comp = compdao.findByComName(danwei);

		TrDao trdao = new TrDao();
		Tr t = trdao.findByTrName(tr);

		if (comp == null) {
			comp = new Comp();
			comp.setId(UUID.randomUUID().toString());
			comp.setCompname(danwei);
			compdao.save(comp.getId(), comp.getCompname());
		}
		if (t == null) {
			t = new Tr();
			t.setId(UUID.randomUUID().toString());
			t.setTrname(tr);
			trdao.save(t.getId(), tr, null);
		}

		saledao.add(UUID.randomUUID().toString(), time, comp.getId(),
				t.getId(), from, total, num, unitmoney);

		HttpSession session = request.getSession();
		int p = 0;
		Object o = session.getAttribute("page");
		if (o != null) {
			p = (Integer) o;
		}

		Page page = new Page(p, 5, saledao.getTotalCount());
		page = saledao.findAlls(page);

		List<String> list = new ArrayList<String>();
		if (page.getMaxPage() < 7) {
			for (int i = 0; i < page.getMaxPage(); i++) {
				list.add(String.valueOf(i + 1));
			}
		} else {
			if ((page.getNowPage()) < 4) {
				for (int i = 1; i < 7; i++) {
					list.add(String.valueOf(i));
				}
			} else {
				if (page.getNowPage() > (page.getMaxPage() - 4)) {
					for (int i = (page.getMaxPage() - 6); i < page.getMaxPage(); i++) {
						list.add(String.valueOf(i + 1));
					}
				} else {
					list.add(String.valueOf(page.getNowPage() - 1));
					list.add(String.valueOf(page.getNowPage()));
					list.add(String.valueOf(page.getNowPage() + 1));
					list.add(String.valueOf(page.getNowPage() + 2));
					list.add(String.valueOf(page.getNowPage() + 3));
					list.add(String.valueOf(page.getNowPage() + 4));
				}
			}
		}

		int sumpeople = 0;
		float sumtotal = 0f;
		for (All a : page.getList()) {
			sumtotal = sumtotal + a.getTotalmoney();
			sumpeople = sumpeople + a.getNum();
		}
		request.setAttribute("pagenum", list);
		request.setAttribute("page", page);
		request.setAttribute("nowpage", page.getNowPage());
		request.setAttribute("all", page.getList());
		request.setAttribute("sum", page.getList().size());
		request.setAttribute("sumtotal", sumtotal);
		request.setAttribute("sumpeople", sumpeople);
		request.setAttribute("unitpeople", sumtotal / sumpeople);
		request.getRequestDispatcher("WEB-INF/view/main.jsp").forward(request,
				response);

		/*
		 * SaleDao sDao = new SaleDao();
		 * saledao.add(UUID.randomUUID().toString(), time, comp.getId(),
		 * t.getId(), from, total, num, unitmoney); List<All> list =
		 * sDao.findAllItems();
		 * 
		 * int sumpeople = 0; float sumtotal = 0f; for (All a : list) { sumtotal
		 * = sumtotal + a.getTotalmoney(); sumpeople = sumpeople + a.getNum(); }
		 * 
		 * 
		 * request.setAttribute("all", list); request.setAttribute("sum",
		 * list.size()); request.setAttribute("sumtotal", sumtotal);
		 * request.setAttribute("sumpeople", sumpeople);
		 * request.setAttribute("unitpeople", sumtotal / sumpeople);
		 * 
		 * request.getRequestDispatcher("WEB-INF/view/main.jsp").forward(request,
		 * response);
		 */

	}
}
